package cn.gowss.vnc;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.websocket.server.WebSocketHandler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;

import cn.gowss.vnc.utils.PropertiesUtils;
import cn.gowss.vnc.ws.WsHandler;

@SpringBootApplication
public class NovncProxyApplication {
	
	public static final int PROXY_PORT = Integer.valueOf(PropertiesUtils.getProperty("vnc.proxy_port"));

	public static void main(String[] args) {
		SpringApplication.run(NovncProxyApplication.class, args);
		try {
			//第一个参数为最大线程数，第二个参数为最小线程数
			QueuedThreadPool threadPool = new QueuedThreadPool(1000, 200);
			Server server = new Server(threadPool);
			ServerConnector connector = new ServerConnector(server);
			connector.setPort(PROXY_PORT);
			server.addConnector(connector);
			WebSocketHandler wsHandler = new WsHandler();
			server.setHandler(wsHandler);
			server.start();
			server.join();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Bean
	public ServletWebServerFactory servletContainer() {
	    JettyServletWebServerFactory jetty = new JettyServletWebServerFactory();
	  //第一个参数为最大线程数，第二个参数为最小线程数
		QueuedThreadPool threadPool = new QueuedThreadPool(1000, 200);
	    jetty.setThreadPool(threadPool);
	    return jetty;
	}

}
